#include "list.h"

List::List ()
{
}

List::~List ()
{
  linkedList.clear ();
}

listIter
List::begin ()
{
  return linkedList.begin ();
}

listIter
List::end ()
{
  return linkedList.end ();
}

void
List::append (Object* node)
{
  linkedList.push_back (node);
}

void
List::insert (Object* node)
{
  linkedList.push_front (node);
}

void
List::remove (int index)
{
  list<Object*>::iterator item = linkedList.begin ();
  while (index-- > 0 && item != linkedList.end ()) item++;
  linkedList.erase (item);
}

bool
List::empty ()
{
  return linkedList.empty ();
}

int
List::size ()
{
  return linkedList.size ();
}

Object*
List::operator[] (int index)
{
  listIter item = linkedList.begin ();
  while (index-- > 0 && item != linkedList.end ())
    item++;
  if (item == linkedList.end ())
    return new Object (); //0;
  return *item;
}

string
List::toString ()
{
  if (linkedList.empty ())
    return string("nil");
  ostringstream buffer;
  buffer << "(";
  listIter item = linkedList.begin ();
  for (int i = 0; i < linkedList.size ()-1; item++, i++)
    {
      if ((*item)->type () == "String")
	buffer << "\"" << (*item)->toString () << "\"";
      else
	buffer << (*item)->toString ();
      buffer << " ";
    }
  buffer << (*item)->toString ();
  buffer << ")";
  return buffer.str ();
}
